package me.zhengjie.ship.shipbase.service.impl;

import me.zhengjie.ship.shipbase.domain.ShipBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.ship.shipbase.repository.ShipBaseRepository;
import me.zhengjie.ship.shipbase.service.ShipBaseService;
import me.zhengjie.ship.shipbase.service.dto.ShipBaseDto;
import me.zhengjie.ship.shipbase.service.dto.ShipBaseQueryCriteria;
import me.zhengjie.ship.shipbase.service.mapper.ShipBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author liwh
* @date 2020-04-22
*/
@Service
//@CacheConfig(cacheNames = "shipBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShipBaseServiceImpl implements ShipBaseService {

    private final ShipBaseRepository shipBaseRepository;

    private final ShipBaseMapper shipBaseMapper;

    public ShipBaseServiceImpl(ShipBaseRepository shipBaseRepository, ShipBaseMapper shipBaseMapper) {
        this.shipBaseRepository = shipBaseRepository;
        this.shipBaseMapper = shipBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ShipBaseQueryCriteria criteria, Pageable pageable){
        Page<ShipBase> page = shipBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shipBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ShipBaseDto> queryAll(ShipBaseQueryCriteria criteria){
        return shipBaseMapper.toDto(shipBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ShipBaseDto findById(Integer id) {
        ShipBase shipBase = shipBaseRepository.findById(id).orElseGet(ShipBase::new);
        ValidationUtil.isNull(shipBase.getId(),"ShipBase","id",id);
        return shipBaseMapper.toDto(shipBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ShipBaseDto create(ShipBase resources) {
        return shipBaseMapper.toDto(shipBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ShipBase resources) {
        ShipBase shipBase = shipBaseRepository.findById(resources.getId()).orElseGet(ShipBase::new);
        ValidationUtil.isNull( shipBase.getId(),"ShipBase","id",resources.getId());
        shipBase.copy(resources);
        shipBaseRepository.save(shipBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shipBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShipBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShipBaseDto shipBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("舷号", shipBase.getShipNo());
            map.put("船型号", shipBase.getType());
            map.put("船名", shipBase.getShipName());
            map.put("舰队", shipBase.getFleet());
            map.put("支队", shipBase.getAddress());
            map.put("服役时间", shipBase.getServerTime());
            map.put("所内序号", shipBase.getSequenceNo());
            map.put("船厂", shipBase.getShipyard());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}